package com.lihao.wanandroid.data

import android.net.Network
import com.lihao.wanandroid.app.network.ApiResponse
import com.lihao.wanandroid.app.network.NetworkApi
import com.lihao.wanandroid.data.model.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext

/**
 * Created by lihao on 2020/5/21.
 * Describe:
 */
object AppRepository {

    /**
     * 登陆
     */
    suspend fun login(userName: String, password: String): ApiResponse<UserInfoResponse> {
        return NetworkApi.instance.service.login(userName, password)
    }

    /**
     * 注册
     */
    suspend fun register(userName: String, password: String, repassword: String): ApiResponse<Any> {
        return NetworkApi.instance.service.register(userName, password, repassword)
    }

    suspend fun logout(): ApiResponse<Any> {
        return NetworkApi.instance.service.logout()
    }

    /**
     * 获取首页banner
     */
    suspend fun getHomeBanner(): ApiResponse<ArrayList<BannerResponse>> {
        return NetworkApi.instance.service.getBanner()
    }

    /**
     * 获取首页文章列表
     */
    suspend fun getHomeData(pageNo: Int): ApiResponse<BasePageResponse<ArticleResponse>> {
        //同时异步请求2个接口，请求完成后合并数据
        return withContext(Dispatchers.IO) {
            val data = async { NetworkApi.instance.service.getArticleList(pageNo) }
            if (pageNo == 0) {
                val topData = async { getTopArticleList() }
                data.await().data.datas.addAll(0, topData.await().data)
                data.await()
            } else {
                data.await()
            }
        }
    }

    /**
     * 获取首页置顶文章
     */
    suspend fun getTopArticleList(): ApiResponse<ArrayList<ArticleResponse>> {
        return NetworkApi.instance.service.getTopArticleList()
    }

    /**
     * 广场列表数据
     */
    suspend fun getPlazaList(pageNo: Int): ApiResponse<BasePageResponse<ArticleResponse>> {
        return NetworkApi.instance.service.getPlazaList(pageNo)
    }

    /**
     * 公众号列表
     */
    suspend fun getPublicAccount(): ApiResponse<ArrayList<PublicAccTitleResponse>> {
        return NetworkApi.instance.service.getPublicAccount()
    }

    /**
     * 公众号历史数据
     */
    suspend fun getPublicData(
        account: String,
        pageNo: Int
    ): ApiResponse<BasePageResponse<ArticleResponse>> {
        return NetworkApi.instance.service.getPublicData(account, pageNo)
    }

    /**
     * 获取最新项目列表
     */
    suspend fun getNewProjectList(
        page: Int
    ): ApiResponse<BasePageResponse<ArticleResponse>> {
        return NetworkApi.instance.service.getNewProjectList(page)
    }

    /**
     * 获取导航数据
     */
    suspend fun getNaviData(): ApiResponse<ArrayList<NavigationResponse>> {
        return NetworkApi.instance.service.getNaviData()
    }

    suspend fun collectArticleIn(id: Int): ApiResponse<Any?> {
        return NetworkApi.instance.service.collectArticleIn(id)
    }

    suspend fun collectArticleOut(
        title: String,
        author: String,
        link: String
    ): ApiResponse<Any?> {
        return NetworkApi.instance.service.collectArticleOut(title, author, link)
    }

    suspend fun uncollectArticle(id: Int): ApiResponse<Any?> {
        return NetworkApi.instance.service.uncollectArticle(id)
    }

    suspend fun collectUrl(
        name: String,
        link: String
    ): ApiResponse<CollectUrlResponse> {
        return NetworkApi.instance.service.collectUrl(name, link)
    }

    suspend fun editCollectUrl(
        id: Int,
        name: String,
        link: String
    ): ApiResponse<Any?> {
        return NetworkApi.instance.service.editCollectUrl(id, name, link)
    }

    suspend fun uncollectUrl(id: Int): ApiResponse<Any?> {
        return NetworkApi.instance.service.uncollectUrl(id)
    }

    suspend fun getTree(): ApiResponse<ArrayList<TreeResponse>> {
        return NetworkApi.instance.service.getTree()
    }

    suspend fun getTreeArticle(
        pageNo: Int,
        cid: Int
    ): ApiResponse<BasePageResponse<ArticleResponse>> {
        return NetworkApi.instance.service.getTreeArticle(pageNo, cid)
    }

    suspend fun searchArticleByAuthor(
        pageNo: Int,
        author: String
    ): ApiResponse<BasePageResponse<ArticleResponse>> {
        return NetworkApi.instance.service.searchArticleByAuthor(pageNo, author)
    }

    suspend fun getProjectTree(): ApiResponse<ArrayList<ProjectTreeResponse>> {
        return NetworkApi.instance.service.getProjectTree()
    }

    suspend fun getProjectList(
        pageNo: Int,
        cid: Int
    ): ApiResponse<BasePageResponse<ArticleResponse>> {
        return NetworkApi.instance.service.getProjectList(pageNo, cid)
    }
}